﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAPDavy.Entities;
using MAPDavy.Repository;
using System.Diagnostics;

namespace MAPDavy.Repository
{
    public class SeccionRepository:MasterRepository,ISeccionRepository
    {

        public List<Seccion> GetAllFromSeccion()
        {
            var query = from s in Context.Seccions
                        .Include("Grade")
                        select s;

            return query.OrderBy(s=>s.GradeId).ToList();
        }

        public List<Seccion> GetFromSeccionByGradeId(int gradeId)
        {
            var query = from s in Context.Seccions
                        where s.GradeId == gradeId
                        select s;

            return query.ToList();
        }


        public List<Seccion> GetFromSeccionByAnioAndGradeId(string anio, int gradeId)
        {
            var query = from s in Context.Seccions
                        join st in Context.SeccionTutors
                        on s.Id equals st.SeccionId
                        where st.Anio == anio && s.GradeId == gradeId
                        select s;

            //EventLog _log = new EventLog();

            //if (!EventLog.SourceExists("MAPDavy"))
            //{
            //    EventLog.CreateEventSource("MAPDavy",
            //    Environment.UserName.ToString());
            //}

            //foreach (var item in query.ToList())
            //{
            //    _log.Source = "MAPDavy";
            //    _log.WriteEntry("Error : " + item.Id.ToString() + " - " + item.ClassName, EventLogEntryType.Information);
            //    _log.Dispose();
            //} 

            return query.ToList();  
        }

        public List<Seccion> GetFromSeccionByAnio(string anio)
        {
            var query = from s in Context.Seccions
                        join st in Context.SeccionTutors
                        on s.Id equals st.SeccionId
                        where st.Anio == anio
                        select s;

            //EventLog _log = new EventLog();

            //if (!EventLog.SourceExists("MAPDavy"))
            //{
            //    EventLog.CreateEventSource("MAPDavy",
            //    Environment.UserName.ToString());
            //}
            
            //foreach (var item in query.ToList()  )
            //{
            //    _log.Source = "MAPDavy";
            //    _log.WriteEntry("Error : " + item.Id.ToString() +" - " + item.ClassName, EventLogEntryType.Warning);
            //    _log.Dispose();
            //}             

            return query.ToList() ; 
        }

        public bool ExisteTutor(string anio, int seccionId)
        {
            var query = (from st in Context.SeccionTutors
                         where st.Anio == anio && st.SeccionId == seccionId
                         select st).SingleOrDefault();

            if (query !=null)
            {
                return true;
            }
            else
	        {
                return false;
        	}
        }


        public Seccion GetFromSeccionById(int id)
        {
            var query = from s in Context.Seccions
                        where s.Id == id
                        select s;

            return query.SingleOrDefault();
        }

        public void AddSeccion(Seccion seccion)
        {
            Context.Seccions.Add(seccion);
            Context.SaveChanges(); 
        }

        public void UpdateSeccion(Seccion seccion)
        {
            var existe = (from s in Context.Seccions
                          where s.Id == seccion.Id
                          select s).SingleOrDefault();

            if (existe != null )
            {
                existe.GradeId = seccion.GradeId;
                existe.ClassName = seccion.ClassName;
                existe.Description = seccion.Description;

                Context.Entry(existe).State = System.Data.EntityState.Modified;
                Context.SaveChanges();
            }
        }

        public void RemoveSeccion(int id)
        {
            var existe = (from s in Context.Seccions
                          where s.Id == id 
                          select s).SingleOrDefault();

            if (existe != null)
            {
                Context.Seccions.Remove(existe);
                Context.SaveChanges();
            }
        }

        public bool CanRemoveSeccion(int id)
        {
            var seccion = (from s in Context.Seccions
                          .Include("SeccionTutors")
                        where s.Id == id
                        select s).SingleOrDefault();

            if (seccion == null)
            {
                return false;
            }

            if (seccion.SeccionTutors.Count> 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
